public class Conversation implements Comparable{
	
	//用户id
	private String identify;
	//会话类型
    private TIMConversationType type;
    //姓名
    protected String name;
	//Tim会话对象
    private TIMConversation conversation;
	//最后一条消息
    private Message lastMessage;
	
	//构造函数
    public NomalConversation(TIMConversation conversation){
        this.conversation = conversation;
        type = conversation.getType();
        identify = conversation.getPeer();
    }
	
	//获取头像
    public String getHeardUrl() {
        switch (type){
            case C2C:
                  return ImageUtil.getAvaterById(identify)+ImageUtil.OSS_ROUND;
            case Group:
               return ImageUtil.GROUP+ImageUtil.OSS_ROUND;
        }
        return ImageUtil.DEFAUT+ImageUtil.OSS_ROUMD;;
    }
	
	//点击进入聊天界面
    public void startChat(Context context) {
		Intent intent=new Intent(context,ChatActivity);
		intent.putExtra("identify",identify);
		intent.putExtra("type",type);
		startActivity(intent);
    }
	
	//最后一条消息set get
    public void setLastMessage(Message lastMessage) {
        this.lastMessage = lastMessage;
    }
    public String getLastMessageSummary(){
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()){
            TextMessage textMessage = new TextMessage(ext.getDraft());
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()){
                return MyApplication.getContext().getString(R.string.conversation_draft) + textMessage.getSummary();
            }else{
                return lastMessage.getSummary();
            }
        }else{
            if (lastMessage == null) return "";
            return lastMessage.getSummary();
        }
    }

    //name的set get方法
	public void setName(String name){
		this.name=name;
	}
    public String getName() {
        if (type == TIMConversationType.Group){
            name=GroupInfo.getInstance().getGroupName(identify);
            if (name.equals("")) name = identify;
        }else{
            FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
            name=profile == null?identify:profile.getName();
        }
        return name;
    }


    //获取未读消息数目
    public long getUnreadNum(){
        if (conversation == null) return 0;
        TIMConversationExt ext = new TIMConversationExt(conversation);
        return ext.getUnreadMessageNum();
    }

    //已读全部消息
    public void readAllMessage(){
        if (conversation != null){
            TIMConversationExt ext = new TIMConversationExt(conversation);
            ext.setReadMessage(null, null);
        }
    }

    //最后一条消息时间
    @Override
    public long getLastMessageTime() {
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()){
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()){
                return ext.getDraft().getTimestamp();
            }else{
                return lastMessage.getMessage().timestamp();
            }
        }
        if (lastMessage == null) return 0;
        return lastMessage.getMessage().timestamp();
    }

    //会话类型获取
    public TIMConversationType getType(){
        return conversation.getType();
    }
	
	//外部获取id
    public String getIdentify(){
        return identify;
    }

	//
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Conversation that = (Conversation) o;
        if (!identify.equals(that.identify)) return false;
        return type == that.type;

    }

	//
    @Override
    public int hashCode() {
        int result = identify.hashCode();
        result = 31 * result + type.hashCode();
        return result;
    }

	//重写compare接口
    @Override
    public int compareTo(Object another) {
        if (another instanceof Conversation){
            Conversation anotherConversation = (Conversation) another;
            long timeGap = anotherConversation.getLastMessageTime() - getLastMessageTime();
            if (timeGap > 0) return  1;
            else if (timeGap < 0) return -1;
            return 0;
        }else{
            throw new ClassCastException();
        }
    }
	
}
